package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
)
import "configuration/service"

func ConfigGet(c *gin.Context) {
	key := c.Param("key")
	data, err := service.ConfigGet(key)
	if err == service.ErrConfigNotFound {
		c.String(http.StatusNotFound, "404 page not found")
	} else if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 2000,
			"errMsg":  err.Error(),
			"data":    data,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 0,
			"errMsg":  "success",
			"data":    data,
		})
	}
}

func ConfigAdd(c *gin.Context) {
	key := c.PostForm("key")
	value := c.PostForm("value")
	err := service.ConfigAdd(key, value)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 2000,
			"errMsg":  err.Error(),
			"data":    nil,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 0,
			"errMsg":  "success",
			"data":    nil,
		})
	}
}

func ConfigEdit(c *gin.Context) {
	key := c.Param("key")
	value := c.PostForm("value")
	err := service.ConfigEdit(key, value)
	if err == service.ErrConfigNotFound {
		c.String(http.StatusNotFound, "404 page not found")
	} else if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 2000,
			"errMsg":  err.Error(),
			"data":    nil,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 0,
			"errMsg":  "success",
			"data":    nil,
		})
	}
}

func ConfigDel(c *gin.Context) {
	key := c.Param("key")
	err := service.ConfigDel(key)
	if err == service.ErrConfigNotFound {
		c.String(http.StatusNotFound, "404 page not found")
	} else if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 2000,
			"errMsg":  err.Error(),
			"data":    nil,
		})
	} else {
		c.JSON(http.StatusOK, gin.H{
			"errCode": 0,
			"errMsg":  "success",
			"data":    nil,
		})
	}
}
